/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.client;

  
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.Window;

public class AnalysisPanelOptionsDialog extends Window
{
	   AnalysisPanel analysisPanel = null;
	    final TextArea whereTextArea = new TextArea();

	    final TextArea orderByTextArea = new TextArea();

	    final TextBox nTextBox = new TextBox();

	    boolean cancelled = false;

	    int type = -1;

	    public AnalysisPanelOptionsDialog(AnalysisPanel analysisPanel, int type, String typeName, String schema) {
	        
	        this.analysisPanel = analysisPanel;
	        
	        final AnalysisPanelOptionsDialog thisDialog = this;
	        
	        this.setShadow(true);
	        this.setFrame(true);
	        
	        this.setIconCls("pac-icon");
	  
	        //center panel  
	        Panel optionsPanel = new Panel();
	        //optionsPanel.setAutoHeight(true);
	        //optionsPanel.setAutoWidth(true);
	        //optionsPanel.set
	  
	        //east panel  
	        Panel schemaPanel = new Panel();  
	        schemaPanel.setTitle("Schema");  
	        //schemaPanel.setAutoHeight(true);
	        //schemaPanel.setAutoWidth(true);
	        //schemaPanel.setWidth("100%");  
	        //schemaPanel.setCollapsible(true);
	        String schemaHTML = "";
	        String[] lines = schema.split(",");
	        for (int i = 0; i < lines.length; i++) {
	            schemaHTML += lines[i] + ",<br>";
	        }
	        schemaPanel.setHtml(schemaHTML);
	  
	        BorderLayoutData centerData = new BorderLayoutData(RegionPosition.CENTER);  
	        centerData.setMargins(3, 0, 3, 3);  
	  
	        BorderLayoutData eastData = new BorderLayoutData(RegionPosition.EAST);  
	        eastData.setSplit(true);  
	        eastData.setMargins(3, 3, 0, 3);  
	        eastData.setCMargins(3, 3, 3, 3);  
	        
	  
	        this.setTitle("Analysis Options for " + typeName);
	        setClosable(true);  
	        setWidth(350);
	      //setAutoWidth(true);
	        setHeight(300);  
	        //setPlain(true);  
	        this.setModal(true);  
	        
	        setLayout(new BorderLayout());  
	        add(optionsPanel, centerData);  
	        add(schemaPanel, eastData);  
	        setCloseAction(Window.HIDE);  
	        
	        this.type = type;
	        final AnalysisPanelOptionsDialog del = this;
	        final int tempType = type;
	        final String tempTypeName = typeName;
	        final String tempSchema = schema;
	        // Set the dialog box's caption.
	        FlexTable table = new FlexTable();
	        // DialogBox is a SimplePanel, so you have to set it's widget property
	        // to
	        // whatever you want its contents to be.

	        int idx = -1;

	        table.setHTML(++idx, 0, "<b>Where</b>");
	        whereTextArea.setVisibleLines(5);
	        table.setWidget(++idx, 0, whereTextArea);

	        table.setHTML(++idx, 0, "<b>Order By</b>");
	        orderByTextArea.setVisibleLines(5);
	        table.setWidget(++idx, 0, orderByTextArea);

	        table.setHTML(++idx, 0, "<b>Results Per Page</b>");
	        nTextBox.setText("15");
	        table.setWidget(++idx, 0, nTextBox);

	        

	        HorizontalPanel buttons = new HorizontalPanel();
	        Button cancelButton = new Button();
	        cancelButton.setText("Cancel");
	        cancelButton.addListener(new ButtonListenerAdapter() {
	            public void onClick(Button button, EventObject obj) {
	                cancelled = true;
	                del.hide();
	            }
	        });
	        
	        Button okButton = new Button();
	        okButton.setText("OK");
	        okButton.addListener(new ButtonListenerAdapter() {
	            public void onClick(Button button, EventObject obj) {
	                cancelled = false;
	                try {
	                    int n = Integer.parseInt(nTextBox.getText());
	                    if (n <= 0)
	                        MessageBox.alert("The value entered in the Results Per Page field must be greater than zero.");
	                    else {
	                        updateOptions(getType(), getWhere(), getOrderBy(), getN());
	                        del.hide();
	                    }
	                } catch (Exception e) {
	                    MessageBox.alert("The value entered in the Results Per Page field is not a valid number.");
	                }
	            }
	        });

	        Button resetButton = new Button();
	        resetButton.setText("Reset");
	        resetButton.addListener(new ButtonListenerAdapter() {
	            public void onClick(Button button, EventObject obj) {
	                whereTextArea.setText("");
	                orderByTextArea.setText("");
	                nTextBox.setText("15");
	            }
	        });
	        
	        //buttons.add(resetButton);
	        //buttons.add(okButton);
	        //buttons.add(cancelButton);
	        this.addButton(resetButton);
	        this.addButton(okButton);
	        this.addButton(cancelButton);
	        
	        table.setWidget(++idx, 0, buttons);
	        //this.setWidget(table);
	        
	        optionsPanel.add(table);

	    }
	    
	    private void updateOptions(int type, String where, String orderBy, int n) {
	        analysisPanel.updateOptions(getType(), getWhere(), getOrderBy(), getN());
	    }
	    
	    public void reset() {
	        this.whereTextArea.setText("");
	        this.orderByTextArea.setText("");
	        this.nTextBox.setText("15");
	    }

	    public String getWhere() {
	        return whereTextArea.getText();
	    }

	    public int getN() {
	        int n = Integer.parseInt(nTextBox.getText());
	        return n;
	    }

	    public String getOrderBy() {
	        return orderByTextArea.getText();
	    }

	    public void setOrderBy(String orderBy) {
	        this.orderByTextArea.setText(orderBy);
	    }

	    public boolean isCancelled() {
	        return cancelled;
	    }

	    public int getType() {
	        return type;
	    }
	
	
}
